package com.android.SchoolManagerSystem;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.SchoolManagerSystem.action.Liveupdate;
import com.android.SchoolManagerSystem.data.Transcript;
import com.android.SchoolManagerSystem.data.Userinfo;

public class SchoolManagerSystem extends Activity implements
		View.OnClickListener {
	OnlineCurriculaPage mOnlineCurriculaPage = null;
	CommonResList mCommonResList = null;
	Userinfo mCurUserinfo = null;
	private static final int QUERYSCORE = 0;
	private static final int QUERYCULAR = QUERYSCORE + 1;
	private static final int QUERYINTERNSHIP = QUERYCULAR + 1;
	public static final boolean bDebug = true;
	public static boolean bLogin = false;

	private String stryear = "2011/2012";
	private String strSem = "1";
	public static int iUserType = 0;
	private String TAG = "DISPLAY";

	private String strFireWebPage = "";

	private int icurquerymode = 0;

	// used for loading page
	Handler handler;
	Handler handlerweb;
	ProgressDialog pd;
	ProgressDialog pdweb;

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Liveupdate liveup = new Liveupdate(this);

		if (bDebug) {
			DisplayMetrics displayMetrics = new DisplayMetrics();

			getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

			Log.i(TAG, "手机屏幕分辨率:" + displayMetrics.widthPixels + "X"

			+ displayMetrics.heightPixels);

			Log.i(TAG, "密度:" + displayMetrics.density);

			Log.i(TAG, "使用每英寸的像素点来显示密度:" + displayMetrics.densityDpi);

			Log.i(TAG, displayMetrics.xdpi + "," + displayMetrics.ydpi);

			Log.i(TAG, displayMetrics.scaledDensity + "");
		}

		ViewGroup rootView = (ViewGroup) findViewById(R.id.operaterlist);
		LayoutInflater inflater = getLayoutInflater();
		inflater.inflate(R.layout.mycenter, rootView);
		inflater.inflate(R.layout.queryconditionpage, rootView);
		inflater.inflate(R.layout.onlinecurricula, rootView);
		inflater.inflate(R.layout.commonreslist, rootView);
		inflater.inflate(R.layout.login, rootView);
		inflater.inflate(R.layout.nav_admin, rootView);
		inflater.inflate(R.layout.scoreinfo, rootView);
		inflater.inflate(R.layout.commonwebpage, rootView);
		inflater.inflate(R.layout.stationmail, rootView);
		inflater.inflate(R.layout.cspwdetail, rootView);
		inflater.inflate(R.layout.lv2_bedroom, rootView);
		inflater.inflate(R.layout.lv2_jiaowxt, rootView);
		inflater.inflate(R.layout.nav_stu, rootView);
		inflater.inflate(R.layout.navmain, rootView);
		inflater.inflate(R.layout.commonwebpagejw, rootView);
		inflater.inflate(R.layout.commonwebpagedg, rootView);
		inflater.inflate(R.layout.nav_bzr, rootView);

		// findViewById(R.id.operaterlist_btn_yiliao).setOnClickListener(this);
		// findViewById(R.id.operaterlist_btn_practive).setOnClickListener(this);

		// mycenter page
		findViewById(R.id.mycenter_btn_backtomain).setOnClickListener(this);
		findViewById(R.id.mycenter_btn_ok).setOnClickListener(this);
		findViewById(R.id.mycenter_btn_cancel).setOnClickListener(this);
		// score query page
		findViewById(R.id.scorequery_btn_backtomain).setOnClickListener(this);
		findViewById(R.id.scorequery_btn_ok).setOnClickListener(this);

		// common web page
		findViewById(R.id.commwebpage_btn_backtomain).setOnClickListener(this);
		findViewById(R.id.commwebpage_btn_preview).setOnClickListener(this);

		findViewById(R.id.commjwwebpage_btn_preview).setOnClickListener(this);
		findViewById(R.id.commjwwebpage_btn_backtomain)
				.setOnClickListener(this);

		findViewById(R.id.commdgwebpage_btn_backtomain)
				.setOnClickListener(this);
		findViewById(R.id.commdgwebpage_btn_preview).setOnClickListener(this);

		// findViewById(R.id.jobinfo_btn_ok).setOnClickListener(this);

		// 学生页面
		findViewById(R.id.stunav_btn_quit).setOnClickListener(this);

		findViewById(R.id.stunav_myinfo).setOnClickListener(this);
		findViewById(R.id.stunav_news).setOnClickListener(this);
		findViewById(R.id.stunav_job).setOnClickListener(this);
		findViewById(R.id.stunav_changepsw).setOnClickListener(this);

		findViewById(R.id.stunav_score).setOnClickListener(this);
		findViewById(R.id.stunav_timetable).setOnClickListener(this);
		findViewById(R.id.stunav_interview).setOnClickListener(this);
		findViewById(R.id.stunav_dormitory).setOnClickListener(this);
		findViewById(R.id.stunav_underwork).setOnClickListener(this);
		findViewById(R.id.stunav_breakrule).setOnClickListener(this);
		findViewById(R.id.stunav_rally).setOnClickListener(this);
		findViewById(R.id.stunav_practice).setOnClickListener(this);

		findViewById(R.id.stunav_epc).setOnClickListener(this);
		findViewById(R.id.stunav_consume).setOnClickListener(this);

		// 老师页面

		findViewById(R.id.bzr_nav_btn_quit).setOnClickListener(this);

		findViewById(R.id.bzr_nav_A1).setOnClickListener(this);
		findViewById(R.id.bzr_nav_A2).setOnClickListener(this);
		findViewById(R.id.bzr_nav_A3).setOnClickListener(this);
		findViewById(R.id.bzr_nav_A4).setOnClickListener(this);

		findViewById(R.id.bzr_nav_B1).setOnClickListener(this);
		findViewById(R.id.bzr_nav_B2).setOnClickListener(this);
		findViewById(R.id.bzr_nav_B3).setOnClickListener(this);
		findViewById(R.id.bzr_nav_B4).setOnClickListener(this);
		findViewById(R.id.bzr_nav_C1).setOnClickListener(this);
		findViewById(R.id.bzr_nav_C2).setOnClickListener(this);
		findViewById(R.id.bzr_nav_C3).setOnClickListener(this);
		findViewById(R.id.bzr_nav_C4).setOnClickListener(this);

		//findViewById(R.id.nav_main).setOnClickListener(this);

		findViewById(R.id.bzr_nav_epc).setOnClickListener(this);
		
		//管理员页面
		
		findViewById(R.id.admin_nav_btn_quit).setOnClickListener(this);

		findViewById(R.id.admin_nav_A1).setOnClickListener(this);
		findViewById(R.id.admin_nav_A2).setOnClickListener(this);
		findViewById(R.id.admin_nav_A3).setOnClickListener(this);
		findViewById(R.id.admin_nav_A4).setOnClickListener(this);

		findViewById(R.id.admin_nav_B1).setOnClickListener(this);
		findViewById(R.id.admin_nav_B2).setOnClickListener(this);
		findViewById(R.id.admin_nav_B3).setOnClickListener(this);
		findViewById(R.id.admin_nav_B4).setOnClickListener(this);
		findViewById(R.id.admin_nav_C1).setOnClickListener(this);
		findViewById(R.id.admin_nav_C2).setOnClickListener(this);
		findViewById(R.id.admin_nav_epc).setOnClickListener(this);

		// 查詢成績頁面
		Spinner spinner = (Spinner) findViewById(R.id.spinner_semer);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter.add(this.getString(R.string.str_up));
		adapter.add(this.getString(R.string.str_down));
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view,
					int position, long id) {
				// Spinner spinner = (Spinner)adapterView;
				// String itemContent =
				// (String)adapterView.getItemAtPosition(position);
				if (position == 0)
					strSem = "1";
				else
					strSem = "2";// 下学期
			}

			@Override
			public void onNothingSelected(AdapterView<?> view) {
				Log.i("SP", view.getClass().getName());
			}
		});

		// scroe info page
		findViewById(R.id.scoreinfo_btn_backtomain).setOnClickListener(this);
		findViewById(R.id.scoreinfo_btn_ok).setOnClickListener(this);

		// online curricula page
		findViewById(R.id.onlinecurricula_btn_backtomain).setOnClickListener(
				this);
		findViewById(R.id.onlinecurricula_btn_ok).setOnClickListener(this);

		// login page
		findViewById(R.id.login_btn_login).setOnClickListener(this);
		findViewById(R.id.login_password_text).setOnClickListener(this);
		findViewById(R.id.login_userid_text).setOnClickListener(this);

		Spinner spinner_ut = (Spinner) findViewById(R.id.spinner_usertype);
		ArrayAdapter<String> adapter_ut = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);
		adapter_ut
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter_ut.add("  学生  ");
		adapter_ut.add("  班主任  ");
		adapter_ut.add("  管理员 ");
		spinner_ut.setAdapter(adapter_ut);
		spinner_ut
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> adapterView,
							View view, int position, long id) {
						switch (position) {
						case 0:
							iUserType = 0;// 学生
							break;
						case 1:
							iUserType = 1;//老师 
							break;
						case 2:
							iUserType = 2;//管理员 
							break;
						default:
							iUserType = 0;
							break;
						}
					}

					@Override
					public void onNothingSelected(AdapterView<?> view) {
						Log.i("SP", view.getClass().getName());
					}
				});

		// common res page
		findViewById(R.id.comrespage_btn_backtomain).setOnClickListener(this);

		PageManager.setRootView(rootView);
		LoadFirstPage();

	}

	private void SaveCommonInfo() {
		Context ctx = this;
		SharedPreferences sp = ctx.getSharedPreferences("SP", MODE_PRIVATE);
		Editor editor = sp.edit();
		CheckBox cbAutoLogin = (CheckBox) findViewById(R.id.checkBox_autoquery);
		CheckBox cbRemeberme = (CheckBox) findViewById(R.id.checkBox_remeberpsw);
		boolean bLogin = false;
		boolean bRemember = false;
		if (cbAutoLogin.isChecked())
			bLogin = true;
		if (cbRemeberme.isChecked())
			bRemember = true;
		if (bLogin || bRemember) {
			editor.putString("USENAME", mCurUserinfo.struserid);
			editor.putString("PSW", mCurUserinfo.struserpsw);
			editor.putString("USERTYPE", String.valueOf(iUserType));
			editor.putBoolean("REMEMBERME", bRemember);
			editor.putBoolean("AUTOLOGIN", bLogin);
		} else {
			editor.putBoolean("REMEMBERME", bRemember);
			editor.putBoolean("AUTOLOGIN", bLogin);
		}
		editor.commit();
	}
	


	private void LoadFirstPage() {
		Context ctx = this;
		SharedPreferences sp = ctx.getSharedPreferences("SP", MODE_PRIVATE);
		if (sp.getBoolean("AUTOLOGIN", false)) {
			// mCurUserinfo.sp.getString("AUTOLOGIN", "u");

			mCurUserinfo = new Userinfo(sp.getString("USENAME", "错误，请手动输入"),
					sp.getString("PSW", "错误，请手动输入"));
			mCurUserinfo.usertype = sp.getString("USERTYPE", "1");

			PostUserLoginInfo(CommonInfo.strLoginUrl,
							mCurUserinfo.struserid, mCurUserinfo.struserpsw,
							mCurUserinfo.usertype);
			PageManager.changeToRootPage();
			
		} else if (sp.getBoolean("REMEMBERME", false)) {
			mCurUserinfo = new Userinfo(sp.getString("USENAME", "错误，请手动输入"),
					sp.getString("PSW", "错误，请手动输入"));
			mCurUserinfo.usertype = sp.getString("USERTYPE", "1");
			EditText txusername = (EditText) findViewById(R.id.login_userid_edit);
			EditText txuserpsw = (EditText) findViewById(R.id.login_password_edit);
			txusername.setText(mCurUserinfo.struserid);
			txuserpsw.setText(mCurUserinfo.struserpsw);
			Spinner dropDUserType = (Spinner) findViewById(R.id.spinner_usertype);
			if ("1".equals(mCurUserinfo.usertype)) {
				dropDUserType.setSelection(0);
			} else {
				dropDUserType.setSelection(1);
			}
			CheckBox cbremember = (CheckBox) findViewById(R.id.checkBox_remeberpsw);
			cbremember.setChecked(true);
			PageManager.changeToPage(PageManager.PAGE_LOGIN);

		} else {
			PageManager.changeToPage(PageManager.PAGE_LOGIN);
		}

	}

	private void initOnlineCurriculaPage() {
		mOnlineCurriculaPage = new OnlineCurriculaPage(this,
				findViewById(R.id.onlinecurricula));

		mOnlineCurriculaPage.resetOnlineCurriculaList(mCurUserinfo.MyCurricula);
	}

	private String gettermstr(String strterm) {
		if (strterm.compareTo("1") == 0)
			return this.getString(R.string.str_up);
		else
			return this.getString(R.string.str_down);
	}

	private void initScoreInfoPage() {
		int iidex = 1;
		if (mCommonResList != null)
			mCommonResList.clear();
		mCommonResList = new CommonResList(this,
				findViewById(R.id.commonreslist));

		for (Transcript score : mCurUserinfo.MyScore) {
			if (score.CCula.strCurriculaName.length() != 0)
				mCommonResList.addcontent(iidex++,
						score.CCula.strCurriculaName, score.strMark, false);
		}

		String strrestitle = this.getString(R.string.str_you) + stryear
				+ gettermstr(strSem)
				+ this.getString(R.string.str_com_info_trans);
		TextView tvcur = (TextView) findViewById(R.id.commonres_title);
		tvcur.setText(strrestitle);

		mCommonResList.resetCommoncontentList();
	}

	private void NavigateToCommWebPage(String comurl) {
		WebView wv;
		strFireWebPage = comurl;
		//if (comurl.contains("wxkj_xgglxt"))
			wv = (WebView) findViewById(R.id.wv);
		//else if (comurl.contains("wxkj_dgsx"))
			//wv = (WebView) findViewById(R.id.wv_dg);
		//else
			//wv = (WebView) findViewById(R.id.wv_jw);
		wv.clearHistory();
		wv.getSettings().setJavaScriptEnabled(true);// 可用JS
		wv.setScrollBarStyle(0);// 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上
		wv.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(final WebView view,
					final String url) {
				loadurl(view, url);// 载入网页
				return true;
			}// 重写点击动作,用webview载入

		});
		wv.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {// 载入进度改变而触发
				if (progress == 100) {
					System.out.println("setWebChromeClient");
					handlerweb.sendEmptyMessage(1);// 如果全部载入,隐藏进度对话框
					view.stopLoading();
				}
				super.onProgressChanged(view, progress);
			}
		});

		pdweb = new ProgressDialog(this);
		pdweb.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pdweb.setMessage("数据载入中，请稍候！");
		handlerweb = new Handler() {
			public void handleMessage(Message msg) {// 定义一个Handler，用于处理下载线程与UI间通讯
				if (!Thread.currentThread().isInterrupted()) {
					switch (msg.what) {
					case 0:
						pdweb.show();// 显示进度对话框
						break;
					case 1:
						pdweb.hide();// 隐藏进度对话框，不可使用dismiss()、cancel(),否则再次调用show()时，显示的对话框小圆圈不会动。
						break;
					}
				}
				super.handleMessage(msg);
			}
		};
		loadurl(wv, comurl);
		PageManager.changeToPage(PageManager.PAGE_COMWEB);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		// BEGIN :add : control UI components to hide
		case KeyEvent.KEYCODE_BACK:
			if (!PageManager.isRootPage()) {
				if (PageManager.isLoginPage()) {
					android.os.Process.killProcess(android.os.Process.myPid());
					System.exit(0);
					return true;
				} else {
					PageManager.changeToRootPage();
					return true;
				}
			}
		}

		return super.onKeyDown(keyCode, event);
	}
//webpage logic
	public void loadurl(final WebView view, final String url) {
		new Thread() {
			public void run() {
				handlerweb.sendEmptyMessage(0);
				view.loadUrl(url);// 载入网页
			}
		}.start();
	}

	private void SetWelComeString() {

		String strwelcome = "您好";
		try {	switch (iUserType) {
		case 0:
			{
				TextView tvcur = (TextView) findViewById(R.id.stu_nav_welcome);
				strwelcome = "您好，学生" ;//+ mCurUserinfo.strusername;
				tvcur.setText(strwelcome);
			}
			break;
		case 1:
			{
				TextView tvcur = (TextView) findViewById(R.id.bzr_nav_welcome);
				strwelcome = "您好，班主任";// + mCurUserinfo.strusername;
				tvcur.setText(strwelcome);
			}
			break;
		case 2:
			{
				TextView tvcur = (TextView) findViewById(R.id.admin_nav_welcome);
				strwelcome = "您好，管理员" ;//+ mCurUserinfo.strusername;
				tvcur.setText(strwelcome);
			}
			break;
		default:
			{
				TextView tvcur = (TextView) findViewById(R.id.stu_nav_welcome);
				strwelcome = "您好，学生";// + mCurUserinfo.strusername;
				tvcur.setText(strwelcome);
			}
			break;
		}
		} catch (Exception ex) {
			TextView tvcur = (TextView) findViewById(R.id.stu_nav_welcome);
			strwelcome = "您好!";
			tvcur.setText(strwelcome);
		}

	}
	
	
	private void PostforSession(String uri, String username, String userpsw,
			String usertype) {
		WebView wv = (WebView) findViewById(R.id.wv);
		wv.clearHistory();
		String postData = "employee.username=" + username + "&employee.password="
				+ userpsw + "&type=" + usertype;
		wv.postUrl(uri, EncodingUtils.getBytes(postData, "BASE64"));
	}

	private void PostUserLoginInfo(String uri, String username, String userpsw,
			String usertype) {
		WebView wv = (WebView) findViewById(R.id.wv);
		wv.clearHistory();
		String postData = "employee.username=" + username + "&employee.password="
				+ userpsw + "&type=" + usertype;
		wv.postUrl(uri, EncodingUtils.getBytes(postData, "BASE64"));
		wv.setWebViewClient(new WebViewClient() {

			
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				WebView wvtmp = (WebView) findViewById(R.id.wv);
				int ia = wvtmp.findAll("success");
				
				//int ia = view.findAll("success");
				if (ia != 0) {
					handler.sendEmptyMessage(1);//登入成功
				} else
					handler.sendEmptyMessage(2);
				// view.loadUrl("about:blank");

			}
		});

	}
	// ---------------button callback-------------------
	
	public void onClick(View v) {
		switch (v.getId()) {
		// student main pageMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
		case R.id.stunav_myinfo: {
			// initCommonResPage();
			// PageManager.changeToPage(PageManager.PAGE_COMMONRESLIST);
			NavigateToCommWebPage(CommonInfo.strstunav_myinfo);
		}
			break;
		case R.id.stunav_news: {
			NavigateToCommWebPage(CommonInfo.strstunav_news);
		}
			break;
		case R.id.stunav_job: {
			NavigateToCommWebPage(CommonInfo.strstunav_job);
		}
			break;
		case R.id.stunav_changepsw: {
			NavigateToCommWebPage(CommonInfo.strModfyPsw);
		}
			break;
		case R.id.stunav_score: {
			NavigateToCommWebPage(CommonInfo.strstunav_score);
		}
			break;
		case R.id.stunav_timetable: {
			//NavigateToCommWebPage(CommonInfo.strstunav_timetable);
		}
			break;
		//case R.id.stunav_interview: {
			//NavigateToCommWebPage(CommonInfo.);
		//}
		//	break;
		case R.id.stunav_dormitory: {
			NavigateToCommWebPage(CommonInfo.strstunav_dormitory);
		}
			break;
		case R.id.stunav_underwork: {
			NavigateToCommWebPage(CommonInfo.strstunav_underwork);
		}
			break;
		case R.id.stunav_breakrule: {
			NavigateToCommWebPage(CommonInfo.strstunav_breakrule);
		}
			break;
		case R.id.stunav_rally: {
			NavigateToCommWebPage(CommonInfo.strstunav_rally);
		}
			break;
		case R.id.stunav_practice: {
			NavigateToCommWebPage(CommonInfo.strstunav_practice);
		}
			break;
		case R.id.stunav_consume: {
			NavigateToCommWebPage(CommonInfo.strstunav_consume);
		}
			break;

		// Teacher MainPage TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
		case R.id.bzr_nav_A1: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_A1);
		}
			break;
		case R.id.bzr_nav_A2: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_A2);
		}
			break;
		case R.id.bzr_nav_A3: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_A3);
		}
			break;
		case R.id.bzr_nav_A4: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_A4);
		}
			break;
		case R.id.bzr_nav_B1: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_B1);
		}
			break;
		case R.id.bzr_nav_B2: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_B2);
		}
			break;
		case R.id.bzr_nav_B3: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_B3);
		}
			break;
		case R.id.bzr_nav_B4: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_B4);
		}
			break;
		case R.id.bzr_nav_C1: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_C1);
		}
			break;
		case R.id.bzr_nav_C2: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_C2);
		}
			break;		
		case R.id.bzr_nav_C3: {
				NavigateToCommWebPage(CommonInfo.str_teachernav_C3);
			}
				break;
		case R.id.bzr_nav_C4: {
			NavigateToCommWebPage(CommonInfo.str_teachernav_C4);
		}
			break;
		//admin page 
			// Teacher MainPage TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
		case R.id.admin_nav_A1: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_A1);
		}
			break;
		case R.id.admin_nav_A2: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_A2);
		}
			break;
		case R.id.admin_nav_A3: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_A3);
		}
			break;
		case R.id.admin_nav_A4: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_A4);
		}
			break;
		case R.id.admin_nav_B1: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_B1);
		}
			break;
		case R.id.admin_nav_B2: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_B2);
		}
			break;
		case R.id.admin_nav_B3: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_B3);
		}
			break;
		case R.id.admin_nav_B4: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_B4);
		}
			break;
		case R.id.admin_nav_C1: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_C1);
		}
			break;
		case R.id.admin_nav_C2: {
			NavigateToCommWebPage(CommonInfo.str_adminnav_C2);
		}
			break;		
			// common page webview
		case R.id.commjwwebpage_btn_preview: {
			WebView wv = (WebView) findViewById(R.id.wv_jw);
			if (wv.canGoBack()) {
				String strurl = wv.getUrl().toString();
				Log.e("WEB-FIRST", strFireWebPage);
				Log.e("WEB-BEFORE", wv.getUrl().toString());
				wv.goBack();
				Log.e("WEB-AFTER", wv.getUrl().toString());
				boolean bres = strFireWebPage.equals(strurl);
				if (bres) {
					PageManager.changeToRootPage();
				}
			} else {
				PageManager.changeToRootPage();
			}
		}
			break;
		case R.id.commdgwebpage_btn_preview: {
			WebView wv = (WebView) findViewById(R.id.wv_dg);
			if (wv.canGoBack()) {
				String strurl = wv.getUrl().toString();
				Log.e("WEB-FIRST", strFireWebPage);
				Log.e("WEB-BEFORE", wv.getUrl().toString());
				wv.goBack();
				Log.e("WEBJW-AFTER", wv.getUrl().toString());
				boolean bres = strFireWebPage.equals(strurl);
				if (bres) {
					PageManager.changeToRootPage();
				}
			} else {
				PageManager.changeToRootPage();
			}
		}
			break;

		case R.id.commwebpage_btn_preview: {
			WebView wv = (WebView) findViewById(R.id.wv);
			if (wv.canGoBack()) {
				String strurl = wv.getUrl().toString();
				Log.e("WEB-FIRST", strFireWebPage);
				Log.e("WEB-BEFORE", wv.getUrl().toString());
				wv.goBack();
				Log.e("WEB-AFTER", wv.getUrl().toString());
				boolean bres = strFireWebPage.equals(strurl);
				if (bres) {
					PageManager.changeToRootPage();
				}
			} else {
				PageManager.changeToRootPage();
			}
		}
			break;

		// login to epc EEEEEEEEEEEEEEEEEEEEEEEE
		case R.id.stunav_epc:
		case R.id.admin_nav_epc:
		case R.id.bzr_nav_epc: {
			try {
				Intent intentecp = getPackageManager()
						.getLaunchIntentForPackage("com.lectek.android.ecp");
				startActivity(intentecp);
			} catch (Exception ex) {
				Uri uri = Uri.parse(CommonInfo.str_epcUri);
				Intent it = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(it);
			}
			break;
		}

		case R.id.comrespage_btn_backtomain:
		case R.id.commdgwebpage_btn_backtomain:
		case R.id.commjwwebpage_btn_backtomain:
		case R.id.commwebpage_btn_backtomain: {
			PageManager.changeToRootPage();
		}
			break;

		// ------------------------------------------student
		// page-------------------------
		case R.id.stunav_btn_quit:
		case R.id.admin_nav_btn_quit:
		case R.id.bzr_nav_btn_quit: {
			PageManager.changeToPage(PageManager.PAGE_LOGIN);
		}
			break;

		// ------------------------------------------teacher
		// page--------------------------

		// login page
		case R.id.login_btn_login: {
			EditText txusername = (EditText) findViewById(R.id.login_userid_edit);
			EditText txuserpsw = (EditText) findViewById(R.id.login_password_edit);
			if (bDebug) {
				// mCurUserinfo = new Userinfo("0712110001", "0712110001");
				if (iUserType == 0)
					mCurUserinfo = new Userinfo("201426630101", "123456");
				else 	if (iUserType == 1)
					mCurUserinfo = new Userinfo("xw", "123456");
				else
					mCurUserinfo = new Userinfo("xg", "123456");
			} else {
				if ((txusername.length() == 0) | (txuserpsw.length() == 0)) {
					Toast.makeText(getApplicationContext(),
							R.string.strcheckinput, Toast.LENGTH_SHORT).show();
					break;
				}

				mCurUserinfo = new Userinfo(txusername.getText().toString(),
						txuserpsw.getText().toString());
			}
			mCurUserinfo.usertype = String.valueOf(iUserType);
			Log.i("UserType", String.valueOf(iUserType));

			handler = new Handler() {
				public void handleMessage(Message msg) {// 定义一个Handler，用于处理下载线程与UI间通讯
					if (!Thread.currentThread().isInterrupted()) {
						switch (msg.what) {
						case 0:
							pd.show();// 显示进度对话框
							break;
						case 1: {
							SaveCommonInfo();
							SetWelComeString();
							PageManager.changeToRootPage();
							pd.hide();// 隐藏进度对话框，不可使用dismiss()、cancel(),否则再次调用show()时，显示的对话框小圆圈不会动。
						}
							break;
						case 2: {
							pd.hide();
							Toast.makeText(getApplicationContext(),
									R.string.strcheckinput, Toast.LENGTH_SHORT)
									.show();
							//PageManager.changeToPage(PageManager.PAGE_COMWEB);
							break;
						}

						}
					}
					super.handleMessage(msg);
				}
			};

			PostUserLoginInfo(CommonInfo.strLoginUrl, mCurUserinfo.struserid,
					mCurUserinfo.struserpsw, String.valueOf(iUserType));
		
			pd = new ProgressDialog(this);
			pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pd.setMessage("登录中，请稍候！");
			pd.show();
			// handler.sendEmptyMessage(0);

		}
			break;
		}
	}
}